package com.wanmao.test;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wanmao.test.model.DefectSearchModel;
import com.wanmao.test.service.ToolShowDemo;
import com.wanmao.test.util.DialogUtils;
import com.wanmao.test.util.StaticUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DefectListActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private ListView lv;
    private String type,SDid,XLid,BZid,BDZid,SBLX,QXZT,QXXZ,TimeStart,TimeEnd;


    private List<DefectSearchModel> defectSearchModelList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_defect_list);
        initView();
    }

    private void initView() {
        lv = (ListView) findViewById(R.id.lv);
        defectSearchModelList =new ArrayList<>();
        type = getIntent().getStringExtra("type");
        if(type.equals("SD")){
            SDid = getIntent().getStringExtra("SDid");
            XLid = getIntent().getStringExtra("XLid");
            MyAsynTask myAsynTask=new MyAsynTask();
            myAsynTask.execute();
        }else if(type.equals("BD")){
            SDid = getIntent().getStringExtra("SDid");
            BZid = getIntent().getStringExtra("BZid");
            BDZid = getIntent().getStringExtra("BDZid");
            SBLX = getIntent().getStringExtra("SBLX");
            QXZT = getIntent().getStringExtra("QXZT");
            QXXZ = getIntent().getStringExtra("QXXZ");
            TimeStart = getIntent().getStringExtra("TimeStart");
            TimeEnd = getIntent().getStringExtra("TimeEnd");
            MyAsynTask myAsynTask=new MyAsynTask();
            myAsynTask.execute();
        }

        findViewById(R.id.img_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }



    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

        String id=defectSearchModelList.get(position).getOBJ_ID();
        Intent i=new Intent(this, DefectDetailActivity.class);
        i.putExtra("OBJ_ID", id);
        i.putExtra("type", type);
        startActivity(i);
    }

    class DefectListAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return defectSearchModelList.size();
        }

        @Override
        public DefectSearchModel getItem(int i) {
            return defectSearchModelList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, final ViewGroup viewGroup) {
            final MyHolder holder;
            if (view == null) {
                view = LayoutInflater.from(DefectListActivity.this).inflate(R.layout.item_defect_search, null);
                holder = new MyHolder(view);
                view.setTag(holder);
            } else {
                holder = (MyHolder) view.getTag();
            }
            holder.tv_device.setText(getItem(i).getDeviceName());
            holder.tv_level.setText(getItem(i).getLevel());
            holder.tv_line.setText(getItem(i).getLine());
            return view;
        }

        class MyHolder {
            public MyHolder(View view) {
                tv_device = (TextView)view.findViewById(R.id.tv_device);
                tv_line = (TextView)view.findViewById(R.id.tv_line);
                tv_level = (TextView)view.findViewById(R.id.tv_level);
            }

            TextView tv_device;
            TextView tv_line;
            TextView tv_level;
        }
    }

    public void getQXList(){
        Map<String, String> map = new HashMap<String, String>();
        map.put("ISC_ID", StaticUtil.ISC_ID);
        map.put("APPID", StaticUtil.APPID);
        map.put("XL_ID", XLid);
        map.put("QXZSB", SDid);
        map.put("pageIndex","1");
        map.put("pageSize", "20");
        String response = ToolShowDemo.manageWebServices("GetQXJL", map);
        Log.e("getQXList", "输电 response："+response);
        JSONObject json = JSON.parseObject(response);

        int result = json.getIntValue("result");
        if (result == 0) {
            String content = json.getString("description");
        } else {
            JSONArray value = json.getJSONArray("resultValue");

            if (value.size() <= 0) {

            } else {
                for (int i = 0; i < value.size(); i++) {
                    JSONObject obj = value.getJSONObject(i);
                    String OBJ_ID = obj.getString("OBJ_ID");
                    String QXZSB = obj.getString("QXZSB");//缺陷主设备
                    String DZORXLMC = obj.getString("DZORXLMC");//电站\线路
                    String DYDJ = obj.getString("DYDJ");//电压等级
                    defectSearchModelList.add(new DefectSearchModel(QXZSB,DZORXLMC,DYDJ,OBJ_ID));

                }
            }
        }

    }


    public void getBDQXList(){
        Map<String, String> map = new HashMap<String, String>();
        map.put("ISC_ID", StaticUtil.ISC_ID);
        map.put("APPID", StaticUtil.APPID);
        map.put("QXZSB", SDid);
        map.put("BDZID", BDZid);
        map.put("FXBZID", BZid);
        map.put("QXZT", QXZT);
        map.put("BDZID", BDZid);
        map.put("SBLX", SBLX);
        map.put("QXXZ", QXXZ);
        map.put("FXRQK", TimeStart);
        map.put("FXRQJ", TimeEnd);
        map.put("pageIndex","1");
        map.put("pageSize", "20");
        String response = ToolShowDemo.manageWebServices("GetSBQXXX", map);
        Log.e("GetSBQXXX", "变电 response："+response);
        JSONObject json = JSON.parseObject(response);

        int result = json.getIntValue("result");
        if (result == 0) {
            String content = json.getString("description");
        } else {
            JSONArray value = json.getJSONArray("resultValue");
            if (value.size() <= 0) {

            } else {
                for (int i = 0; i < value.size(); i++) {
                    JSONObject obj = value.getJSONObject(i);
                    String QXID = obj.getString("QXID");//缺陷id
                    String QXZSB = obj.getString("QXZSB");//缺陷主设备
                    String DZORXLMC = obj.getString("DZORXLMC");//电站\线路
                    String DYDJ = obj.getString("DYDJ");//电压等级
                    defectSearchModelList.add(new DefectSearchModel(QXZSB,DZORXLMC,DYDJ,QXID));

                }
            }
        }

    }

    class MyAsynTask extends AsyncTask<String, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            DialogUtils.startProgressDialog(DefectListActivity.this,"正在加载");
            defectSearchModelList = new ArrayList<>();

        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            if( defectSearchModelList.size()<=0 ){
                Toast.makeText(DefectListActivity.this, "暂无缺陷信息", Toast.LENGTH_SHORT).show();
                finish();
            }
            lv.setAdapter(new DefectListAdapter());
            lv.setOnItemClickListener(DefectListActivity.this);
            DialogUtils.stopProgressDialog();
        }
        @Override
        protected Void doInBackground(String... params) {
            if(type.equals("SD")){
                getQXList();
            }else if(type.equals("BD")){
                getBDQXList();
            }
            return null;
        }
    }

}
